﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GMS.BIZ;
using System.Collections;

namespace Web.Information
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!((Account)Session["Account"]).RoleCurrent.Equals('s')) Response.Redirect("Default.aspx");
            //初始化学生求职意向信息
            Student myStudent = (Student)Session["Student"];
            if (myStudent.StudentName == "")
                Response.Redirect("StuInformation.aspx");
            GridView_Job.DataSource = myStudent.Jobs.GetDataSet();
            GridView_Job.DataBind();

            //绑定DropDownList数据
            if (!IsPostBack)
            {
                ArrayList arraylist = new ArrayList();
                arraylist = Library.GetJobLib();
                for (int i = 0; i < arraylist.Count; i++)
                {
                    ListItem myListItem = new ListItem();
                    myListItem = (ListItem)arraylist[i];
                    DropDownList_joblevel.Items.Add(myListItem);
                }
                ListItem otherListItem = new ListItem();
                otherListItem.Text = "其它";
                otherListItem.Value = "others";
                DropDownList_joblevel.Items.Add(otherListItem);     

            }


            if (DropDownList_joblevel.SelectedValue == "others")
            {
                Label_nolist.Visible = true;
                TextBox_nolist.Visible = true;
                RequiredFieldValidator_nolist.Enabled = true;
            }
            else
            {
                Label_nolist.Visible = false;
                TextBox_nolist.Visible = false;
                RequiredFieldValidator_nolist.Enabled = false;
            }
            #region ViewState

            //初始化ViewState       
            if (ViewState["status"] == null)
                ViewState["status"] = 1;

            this.ViewStateControls(int.Parse(ViewState["status"].ToString()));

            #endregion ViewState
        }
        protected void ViewStateControls(int i)
        {
            if (i == 1)//添加
            {
                ImageButton_Cancel.Visible = false;
                ImageButton_Add.Visible = true;
                ImageButton_Edit.Visible = false;
                ImageButton_Delete.Visible = false;
            }
            else if (i == 2)//修改
            {
                ImageButton_Cancel.Visible = true;
                ImageButton_Add.Visible = false;
                ImageButton_Edit.Visible = true;
                ImageButton_Delete.Visible = false;
            }
            else if (i == 3) //删除
            {
                ImageButton_Cancel.Visible = true;
                ImageButton_Add.Visible = false;
                ImageButton_Edit.Visible = false;
                ImageButton_Delete.Visible = true;
            }
        }

        //GridView的修改按钮
        protected void GridViewButton_Edit_Click(object sender, ImageClickEventArgs e)
        {
            Job myJob = new Job();
            Student myStudent = (Student)Session["Student"];
            myJob = myStudent.Jobs.GetOne(decimal.Parse(((ImageButton)sender).CommandArgument));
            Label_JobID.Text = myJob.id.ToString();
           
            
         
            ViewState["status"] = 2;
            Label_result.Text = "";

            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = myJob.job_level + " " + ((DateTime)myJob.job_time).ToShortDateString();
        }

        //GridView的删除按钮
        protected void GridViewButton_Delete_Click(object sender, ImageClickEventArgs e)
        {
            Job myJob = new Job();
            Student myStudent = (Student)Session["Student"];
            myJob= myStudent.Jobs.GetOne(decimal.Parse(((ImageButton)sender).CommandArgument));
            Label_JobID.Text = myJob.id.ToString();
            
            
            
            ViewState["status"] = 3;
            Label_result.Text = "";

            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = myJob.job_level + " " + ((DateTime)myJob.job_time).ToShortDateString();
        }

        //修改按钮
        protected void ImageButton_Edit_Click(object sender, ImageClickEventArgs e)
        {
            Job myJob = new Job();
            Student myStudent1 = (Student)Session["Student"];
            myJob.id = decimal.Parse(Label_JobID.Text.ToString());
            if (TextBox_nolist.Text == null || TextBox_nolist.Text == string.Empty)
                myJob.job_level = DropDownList_joblevel.SelectedValue;
            else
            {
               
                myJob.job_level = TextBox_nolist.Text.ToString();
            }
            myJob.job_time= DateTime.Today;
            string str = myStudent1.Jobs.Update(myJob);

            if (str.CompareTo(Resources.ErrorMessage.OK) == 0)
            {
                Label_result.Text = Resources.ErrorMessage.OK;
                Student myStudent2 = (Student)Session["Student"];
                GridView_Job.DataSource = myStudent2.Jobs.GetDataSet();
                GridView_Job.DataBind();
            }
            else Label_result.Text = Resources.ErrorMessage.DBOptError;

            ViewState["status"] = 1;
           

            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = "";
        }

        //删除按钮
        protected void ImageButton_Delete_Click(object sender, ImageClickEventArgs e)
        {
            Job myJob = new Job();
            Student myStudent1 = (Student)Session["Student"];
            string str = myStudent1.Jobs.Delete(decimal.Parse(Label_JobID.Text.ToString()));

            if (str.CompareTo(Resources.ErrorMessage.OK) == 0)
            {
                Label_result.Text = Resources.ErrorMessage.OK;
                Student myStudent2 = (Student)Session["Student"];
                GridView_Job.DataSource = myStudent2.Jobs.GetDataSet();
                GridView_Job.DataBind();
            }
            else Label_result.Text = Resources.ErrorMessage.DBOptError;

            ViewState["status"] = 1;

            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = "";
        }


        //添加按钮
        protected void ImageButton_Add_Click(object sender, ImageClickEventArgs e)
        {
            Job myJob = new Job();
            Student myStudent1 = (Student)Session["Student"];
            if (TextBox_nolist.Text == null || TextBox_nolist.Text == string.Empty)
                myJob.job_level = DropDownList_joblevel.SelectedValue;
            else
            {
             
                myJob.job_level = TextBox_nolist.Text.ToString();
            }
            myJob.job_time = DateTime.Today;
            string str = myStudent1.Jobs.Add(myJob);

            if (str.CompareTo(Resources.ErrorMessage.OK) == 0)
            {
                Label_result.Text = Resources.ErrorMessage.OK;
                Student myStudent2 = (Student)Session["Student"];
                GridView_Job.DataSource = myStudent2.Jobs.GetDataSet();
                GridView_Job.DataBind();
            }
            else Label_result.Text = Resources.ErrorMessage.DBOptError;

            ViewState["status"] = 1;

            
            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = "";
        }

        protected void ImageButton_Cancel_Click(object sender, ImageClickEventArgs e)
        {
           
            ViewState["status"] = 1;

            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = "";
        }

        protected void DropDownList_joblevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList_joblevel.SelectedValue == "others")
            {
                Label_nolist.Visible = true;
                TextBox_nolist.Visible = true;
                RequiredFieldValidator_nolist.Enabled = true;
            }
            else
            {
                Label_nolist.Visible = false;
                TextBox_nolist.Visible = false;
                RequiredFieldValidator_nolist.Enabled = false;
            }
        }
    }
}
